package com.asion.qkan.adapters;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.entry.HomeBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by HCJ on 2016/10/13.
 */

public class HomeRecyAdapter extends RecyclerView.Adapter<HomeRecyAdapter.ViewHolder>{
    private List<HomeBean.DataBean.ComicsBean> list;
    private Context context;
    private int type;

    public void setType(int type) {
        this.type = type;
    }

    public HomeRecyAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void addAll(Collection collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }
    public void clean(){
        list.clear();
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater from = LayoutInflater.from(context);
        View view = from.inflate(R.layout.item_home_recy, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HomeBean.DataBean.ComicsBean comicsBean = list.get(position);
        holder.item_label_text.setText(comicsBean.getLabel_text());

        //将字符串装换成颜色
        int color = Color.parseColor(comicsBean.getLabel_color());
        holder.item_label_text.setBackgroundColor(color);
        holder.item_title.setText(comicsBean.getTopic().getTitle());
        holder.item_nickname.setText(comicsBean.getTopic().getUser().getNickname());

        Uri uri = Uri.parse(comicsBean.getCover_image_url());
        holder.item_cover_image_url.setImageURI(uri);

        holder.item_subtitle.setText(comicsBean.getTitle());
        holder.item_likes_count.setText(comicsBean.getLikes_count()+"");
        holder.item_comments_count.setText(comicsBean.getComments_count()+"");

        //设置点击事件
        holder.item_label_text.setOnClickListener(comicsBean.getTopic());
        holder.item_title.setOnClickListener(comicsBean.getTopic());

        holder.item_nickname.setOnClickListener(comicsBean.getTopic().getUser());
        holder.item_cover_image_url.setOnClickListener(comicsBean);
        holder.item_subtitle.setOnClickListener(comicsBean);
        holder.item_likes_count.setOnClickListener(comicsBean);
        holder.item_comments_count.setOnClickListener(comicsBean);
    }

    @Override
    public int getItemCount() {
        return list == null?0:list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView item_label_text,item_title,item_nickname;
        private SimpleDraweeView item_cover_image_url;
        private TextView item_subtitle,item_likes_count,item_comments_count;
        public ViewHolder(View itemView) {
            super(itemView);
            item_label_text = (TextView) itemView.findViewById(R.id.item_label_text);
            item_title = (TextView) itemView.findViewById(R.id.item_title);
            item_nickname = (TextView) itemView.findViewById(R.id.item_nickname);
            item_cover_image_url = (SimpleDraweeView) itemView.findViewById(R.id.item_cover_image_url);
            item_subtitle = (TextView) itemView.findViewById(R.id.item_subtitle);
            item_likes_count = (TextView) itemView.findViewById(R.id.item_likes_count);
            item_comments_count = (TextView) itemView.findViewById(R.id.item_comments_count);
        }
    }
}
